//
//  BasicAnimationView.m
//  AnimationDemo
//
//  Created by Jiaozl 2018 on 2018/9/11.
//  Copyright © 2018年 Jiaozl. All rights reserved.
//

#import "BasicAnimationView.h"

@implementation BasicAnimationView

- (void)drawRect:(CGRect)rect {
    [super drawRect:rect];
    
    [self rotationAnimation];
    
    [self addScaleAnimation];

    [self addGroupAnimation];
    
    [self drawLineAnimation];
}






#pragma mark - CABasicAnimation
/** 缩放、旋转、平移、背景色 */
- (void)addGroupAnimation {
    // 添加动画层
    CALayer *groupLayer = [[CALayer alloc] init];
    groupLayer.backgroundColor = UIColor.redColor.CGColor;
    groupLayer.frame = CGRectMake(60, 100, 50, 50);
    groupLayer.cornerRadius =  10;
    [self.layer addSublayer:groupLayer];
    
    // 动画
    // 缩放
    CABasicAnimation *scaleAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    scaleAnimation.fromValue = [NSNumber numberWithFloat:1.0];
    scaleAnimation.toValue = [NSNumber numberWithFloat:1.5];
    scaleAnimation.autoreverses = YES;
    scaleAnimation.repeatCount = MAXFLOAT;
    scaleAnimation.duration = 0.8;
    
    // 移动
    CABasicAnimation *moveAnimation = [CABasicAnimation animationWithKeyPath:@"position"];
    moveAnimation.fromValue = [NSValue valueWithCGPoint:groupLayer.position];
    moveAnimation.toValue = [NSValue valueWithCGPoint:CGPointMake(groupLayer.position.x+200, groupLayer.position.y)];
    moveAnimation.autoreverses = YES;
    moveAnimation.repeatCount = MAXFLOAT;
    moveAnimation.duration = 2;
    
    // 旋转
    CABasicAnimation *rotateAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    rotateAnimation.fromValue = [NSNumber numberWithFloat:0.0];
    rotateAnimation.toValue = [NSNumber numberWithFloat:2.0 * M_PI];
    rotateAnimation.autoreverses = YES;
    rotateAnimation.repeatCount = MAXFLOAT;
    rotateAnimation.duration = 2;
    
    // 颜色变化
    CABasicAnimation *colorAnimation = [CABasicAnimation animationWithKeyPath:@"backgroundColor"];
    colorAnimation.fromValue = (__bridge id)(UIColor.redColor.CGColor);
    colorAnimation.toValue = (__bridge id)(UIColor.greenColor.CGColor);
    colorAnimation.autoreverses = YES;
    colorAnimation.repeatCount = MAXFLOAT;
    colorAnimation.duration = 2;
    
    // 将各个animation组合放入groupAnimation
    CAAnimationGroup *groupAnimation = [CAAnimationGroup animation];
    groupAnimation.animations = @[scaleAnimation, moveAnimation, rotateAnimation, colorAnimation];
    groupAnimation.duration = 2.0;
    groupAnimation.autoreverses = YES;
    groupAnimation.repeatCount = MAXFLOAT;
    
    // 将动画组添加到layer
    [groupLayer addAnimation:groupAnimation forKey:@"groupAnimation"];
}



/** 缩放 */
- (void)addScaleAnimation {
    // 添加动画层
    CALayer *scaleLayer = [[CALayer alloc] init];
    scaleLayer.backgroundColor = UIColor.redColor.CGColor;
    scaleLayer.frame = CGRectMake(60, 20, 50, 50);
    scaleLayer.cornerRadius =  10;
    [self.layer addSublayer:scaleLayer];
    
    // 动画
    CABasicAnimation *scaleAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    scaleAnimation.fromValue = [NSNumber numberWithFloat:1.0];
    scaleAnimation.toValue = [NSNumber numberWithFloat:1.5];
    scaleAnimation.autoreverses = YES;
    scaleAnimation.fillMode = kCAFillModeBoth;
    scaleAnimation.removedOnCompletion = NO;
    scaleAnimation.repeatCount = MAXFLOAT;
    scaleAnimation.duration = 0.8;
    
    /*
     kCAFillModeRemoved 这个是默认值,也就是说当动画开始前和动画结束后,动画对layer都没有影响,动画结束后,layer会恢复到之前的状态
     kCAFillModeForwards 当动画结束后,layer会一直保持着动画最后的状态
     kCAFillModeBackwards 这个和kCAFillModeForwards是相对的,就是在动画开始前,你只要将动画加入了一个layer,layer便立即进入动画的初始状态。因为有可能出现fromValue不是目前layer的初始状态的情况，如果fromValue就是layer当前的状态，则这个参数就没太大意义。
     kCAFillModeBoth 理解了上面两个,这个就很好理解了,这个其实就是上面两个的合成.动画加入后开始之前,layer便处于动画初始状态,动画结束后layer保持动画最后的状态.
     */
    
    // 添加动画
    [scaleLayer addAnimation:scaleAnimation forKey:@"scaleAnimation"];
}


/** 内容变换动画  */
- (void)rotationAnimation {
    // 添加动画层
    CALayer *rotationLayer = [[CALayer alloc] init];
    rotationLayer.backgroundColor = UIColor.redColor.CGColor;
    rotationLayer.frame = CGRectMake(240, 20, 100, 100);
    rotationLayer.cornerRadius =  10;
    [self.layer addSublayer:rotationLayer];
    
    // 动画
    CGImageRef fromimage = [UIImage imageNamed:@"from"].CGImage;
    CGImageRef toimage = [UIImage imageNamed:@"to"].CGImage;
    CABasicAnimation *contentsAnim = [CABasicAnimation animationWithKeyPath:@"contents"];
    contentsAnim.fromValue = (__bridge id)fromimage;
    contentsAnim.toValue = (__bridge id)toimage;
    contentsAnim.duration = 4.0;
    contentsAnim.autoreverses = YES;
    contentsAnim.repeatCount = MAXFLOAT;
    contentsAnim.fillMode = kCAFillModeBoth;
    contentsAnim.removedOnCompletion = NO; // 有时程序从后台进入，动画就不见了，加上这句就好了
    
    // 添加动画
    [rotationLayer addAnimation:contentsAnim forKey:@"contentsAnim"];
}

- (void)drawLineAnimation {
    UIBezierPath *ovalPath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(100, 200, 200, 300)];
    
    // 添加绘图层及路径， 当然也可以在当前图层通过获取图形上下文绘制
    CAShapeLayer *shapeLayer = [[CAShapeLayer alloc] init];
    shapeLayer.strokeColor = UIColor.redColor.CGColor;
    shapeLayer.fillColor = UIColor.clearColor.CGColor;
    shapeLayer.lineWidth = 2.0;
    shapeLayer.lineJoin = kCALineJoinRound;
    shapeLayer.lineCap = kCALineCapRound;
    shapeLayer.path = ovalPath.CGPath;
    [self.layer addSublayer:shapeLayer];
    
    // 添加动画
    CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
    pathAnimation.duration = 5.0;
    pathAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    pathAnimation.fromValue = [NSNumber numberWithFloat:0.0];
    pathAnimation.toValue = [NSNumber numberWithFloat:1.0];
    pathAnimation.autoreverses = YES;
    pathAnimation.fillMode = kCAFillModeForwards;
    pathAnimation.repeatCount = MAXFLOAT;
    [shapeLayer addAnimation:pathAnimation forKey:@"pathStrokeEnd"];
    
}

@end
